package cn.four.hqServer.modules.userlogin.service.impl;


import cn.four.hqServer.common.entity.HqCardInfo;
import cn.four.hqServer.common.entity.HqUserInfo;
import cn.four.hqServer.common.enums.CardStatusEnum;
import cn.four.hqServer.common.utils.RedisUtil;
import cn.four.hqServer.modules.transfer.service.ZnbIHqCardInfoService;
import cn.four.hqServer.modules.transfer.service.ZnbIHqUserInfoService;
import cn.four.hqServer.modules.userlogin.dto.UserNumDto;
import cn.four.hqServer.modules.userlogin.mapper.ZxUserLoginMapper;
import cn.four.hqServer.modules.userlogin.service.ZxUserLoginService;
import cn.four.hqServer.modules.userlogin.vo.UserLoginVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;


@Service
public class ZxUserLoginServiceImpl implements ZxUserLoginService {
    @Autowired
    private ZxUserLoginMapper zxUserLoginMapper;

    @Autowired
    private ZnbIHqUserInfoService userInfoService;

    @Autowired
    private ZnbIHqCardInfoService cardInfoService;

    @Autowired
    RedisUtil redisUtil;

    /**
     * 登录方法
     */
    @Override
    public UserLoginVo login(String userId, String password) {
        //判断该用户的身份证号和密码是否正确，正确的话返回“1”
//        String s = zxUserLoginMapper.selectUsernameByUseridPassword(userId,password);
        HqUserInfo userInfo = userInfoService.getById(userId);
        String pwd = DigestUtils.md5Hex(
                password + userInfo.getSalt()
        );
        if(!userInfo.getPassword().equals(pwd)){
            return null;
        }
        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setUserId(userId);
        HqCardInfo cardInfo = cardInfoService.getOne(new QueryWrapper<HqCardInfo>()
                .eq("user_id",userId).ne("state", CardStatusEnum.logout.getCode()));
        if(cardInfo !=null){
            userLoginVo.setCardId(cardInfo.getCardId());
        }
        userLoginVo.setUserName(userInfo.getUsername());
        return userLoginVo;
    }
    /**
     * 判断手机号码是否存在
     */

    @Override
    public Boolean selectUsernameByNumber(String number) {
        String s = zxUserLoginMapper.selectUsernameByNumber(number);
        return s != null;
    }
    /**
     * 判断身份证号是否存在
     */

    @Override
    public Boolean selectUsernameByUserId(String userId) {
        return zxUserLoginMapper.selectUsernameByUserId(userId) != null;
    }

    @Override
    public boolean isLogin(String phone) {
        //判断手机号是否存在
        HqUserInfo userInfo = userInfoService.getOne(new QueryWrapper<HqUserInfo>()
                .eq("number", phone));
        if(userInfo == null){
            return false;
        }
        // 生成6位随机数
        Random random = new Random();
        int num = random.nextInt(900000) + 100000;
        System.out.println(num);
        //存入redis
        redisUtil.set(phone,String.valueOf(num));

        return true;
    }

    @Override
    public boolean loginByPhone(UserNumDto userNumDto) {
        //获取
        //存入redis
        String code = (String)redisUtil.get(userNumDto.getPhone());
        if(userNumDto.getPassword().equals(code)){
            return true;
        }
        return false;
    }

    @Override
    public UserLoginVo getUserInfo(String phone) {
        UserLoginVo userLoginVo = new UserLoginVo();
        HqUserInfo number = userInfoService.getOne(new QueryWrapper<HqUserInfo>()
                .eq("number", phone));
        userLoginVo.setUserId(number.getUserId());
        userLoginVo.setUserName(number.getUsername());
        userLoginVo.setCardId(cardInfoService.getOne(new QueryWrapper<HqCardInfo>()
                .eq("user_id",number.getUserId())).getCardId());
        return userLoginVo;
    }
}
